package com.ems.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ems.dao.*;
import com.ems.dto.ErrorMsg;
import com.ems.entity.Admin;
import com.ems.entity.Exam;
import com.ems.entity.StuExam;
import com.ems.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MyDtoMapper myDtoMapper;

    @Autowired
    StudentDao studentDao;

    @Autowired
    StuExamDao stuExamDao;

    @Autowired
    ExamDao examDao;

    @Autowired
    AdminDao adminDao;

    @Override
    public ErrorMsg errorMsg() {
        List<ErrorMsg.errorMsg> errorMsgs = myDtoMapper.errorMsg();

        ErrorMsg errorMsg = new ErrorMsg();
        errorMsg.setErrorMsg(errorMsgs);
        return errorMsg;
    }

    @Override
    public boolean addstu(Integer eid, String username) {

        StuExam stuExam = new StuExam();

        //查询是否存在该学生
        Integer stuid = studentDao.queryIDbyUsername(username);
        if(stuid == null)
        {
            return false;
        }

        //查询是否已经被绑定
        Integer exit = stuExamDao.queryIDByeid_stuid(eid, stuid);
        if(exit != null)
        {
            return false;
        }

        stuExam.setEid(eid);
        stuExam.setStuid(stuid);
        stuExam.setIsjoin(0);
        return stuExamDao.insert(stuExam) != 0;
    }

    @Override
    public boolean updateExam(JSONObject jsonObject) {
        Exam exam = new Exam();
        Date startTime = new Date();
        Date endTime = new Date();

        //处理JSON字符串
        jsonObject = jsonObject.getJSONObject("data");
        Integer eid = jsonObject.getInteger("eid");
        Integer epid = jsonObject.getInteger("epid");
        String title = jsonObject.getString("title");
        String subject = jsonObject.getString("subject");
        String className = jsonObject.getString("className");
        String start_time = jsonObject.getString("start_time");
        String end_time = jsonObject.getString("end_time");
        String invigilatorA = jsonObject.getString("invigilatorA");
        String invigilatorB = jsonObject.getString("invigilatorB");
        String describes = jsonObject.getString("describes");

        //日期类格式
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //处理异常
        try {
            startTime = sf.parse(start_time);
            endTime = sf.parse(end_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //设置实体类
        exam.setEpid(epid);
        exam.setId(eid);
        exam.setDescribes(describes);
        exam.setTitle(title);
        exam.setSubject(subject);
        exam.setClassName(className);
        exam.setInvigilatorA(invigilatorA);
        exam.setInvigilatorB(invigilatorB);
        exam.setStartTime(startTime);
        exam.setEndTime(endTime);

        System.out.println(exam);
        if(examDao.update(exam) != 0)
        {
            return true;
        }
        return false;
    }

    //猫
    @Override
    public Admin queryByUsername(String username) {
        return adminDao.queryByUsername(username);
    }
}
